var addDormList = function (pageNumber, pageSize, tungNum, roomNum, callback) {
    var loadDiv = load("#dormListTable", 99999);
    isNulltoWhat(pageSize, 8);
    isNulltoWhat(pageNumber, 1);
    tungNum = isnullTONull(tungNum);
    roomNum = isnullTONull(roomNum);
    $.ajax({
        url: "dorm/list",
        type: "post",
        data: {
            "token": localStorage['token'],
            "pageSize": pageSize,
            "pageNumber": pageNumber,
            "tungNum": tungNum,
            "roomNum": roomNum
        },
        success: function (data) {
            $("#dormListTable").children("tbody").html("");
            $(".chooseNum").children("select").html("");
            if (data.code === 200) {
                listSetting(data.data.totalPages, data.data.pageNumber);
                localStorage['pageSize'] = data.data.pageSize + "";
                localStorage['pageNumber'] = data.data.pageNumber + "";
                var length = data.data.content.length;
                for (var j = 0; j < data.data.totalPages; j++) {
                    var option = addRealOption(j + 1);
                    if ((j + 1) == data.data.pageNumber) {
                        option.attr("selected", "selected");
                    }
                    option.appendTo($(".chooseNum").children("select"));
                }

                for (var i = 0; i < length; i++) {
                    var dorm = data.data.content[i];
                    var tr = $('<tr></tr>');
                    addTd(dorm.tungNum).appendTo(tr);
                    addTd(dorm.roomNum).appendTo(tr);
                    $(' <td>\n' + ' <button  onclick="dormDetail($(this))" >查看</button>\n' + ' <button class="redButton">删除</button> </td>').appendTo(tr);
                    addTdHidId(dorm.id).appendTo(tr);
                    addTdHidId(dorm.tungId).appendTo(tr);
                    addTdHidId(dorm.roomId).appendTo(tr);
                    tr.appendTo($("#dormListTable").children("tbody"))
                }
                $(".thisNum").children("span").text(data.data.pageNumber);
                if ($("#dormListTable").children("tbody").text() === "") {
                    var bk = getNothingBackground();
                    bk.appendTo($("#dormListTable").children("tbody"));
                }
                dialog_close();
                if ($("#dormListTable").children("tbody").text() === "") {
                    var bk = getNothingBackground();
                    bk.appendTo($("#dormListTable").children("thead"));
                }
            } else {
                errorMessage(data.msg);
            }

            if (typeof callback === "function") {
                callback()
            }
            setTimeout(function () {
                load_close(loadDiv);
            }, 50);
        }
    })
};

$("#next").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) + 1;
        console.log(pageNumber);
        addDormList(pageNumber, localStorage['pageSize'], localStorage['tungNum'], localStorage['roomNum'], function () {
            tableChange("#dormListTable");
        });
    }

});

$("#last").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) - 1;
        addDormList(pageNumber, localStorage['pageSize'], localStorage['tungNum'], localStorage['roomNum'], function () {
            tableChange("#dormListTable");
        });
    }

});

$(".chooseNum").children("select").change(function () {
    addDormList($(this).val(), localStorage['pageSize'], localStorage['tungNum'], localStorage['roomNum'], function () {
        tableChange("#dormListTable");
    });
});

$("#saveTung").click(function () {
    dialog_show();
    $(".dialog").load("page/admin/dormSave");
    localStorage['flag'] = "tung";
});

$("#saveRoom").click(function () {
    dialog_show();
    $(".dialog").load("page/admin/dormSave");
    localStorage['flag'] = "room";
});

$("#saveDrom").click(function () {
    dialog_show();
    $(".dialog").load("page/admin/dormSave");
    localStorage['flag'] = "dorm";
});


$("#dormHighSearchBtn").click(function () {
    dialog_show();
    $(".dialog").load("page/admin/dormHighSearch");
});

var dormDetail = function (btn) {
    url = "page/admin/dormDetail";
    $("#body").load(url);
    var dormId = btn.parent().next().text();
    localStorage['dormId'] = dormId;

};

var dormSaveLoad = function () {
    var flag = localStorage['flag'];
    if (flag === "tung") {
        $("#saveTungNumContent").show();
        $("#saveRoomNumContent").hide();
        $("#saveDormContent").hide();
    }
    if (flag === "room") {
        $("#saveTungNumContent").hide();
        $("#saveRoomNumContent").show();
        $("#saveDormContent").hide();
    }
    if (flag === "dorm") {
        $("#saveTungNumContent").hide();
        $("#saveRoomNumContent").hide();
        $("#saveDormContent").show();
        addTungAndRoom();
    }
    localStorage.removeItem("flag");
};

var addTungAndRoom = function () {
    $.ajax({
        url: "dorm/tungAndRoom",
        type: "post",
        data: {
            "token": localStorage['token']
        },
        success: function (data) {
            $("#tungNumSelect").next().next().next().children("ul").text("");
            $("#roomNumSelect").next().next().next().children("ul").text("");
            if (data.code === 200) {
                var tungs = data.data.tungs;
                var rooms = data.data.rooms;
                for (var i = 0; i < rooms.length; i++) {
                    var room = rooms[i];
                    var li1 = addOption(room.num, room.id);
                    li1.appendTo($("#roomNumSelect").next().next().next().children("ul"));

                }
                for (var j = 0; j < tungs.length; j++) {
                    var tung = tungs[j];
                    var li2 = addOption(tung.num, tung.id);
                    li2.appendTo($("#tungNumSelect").next().next().next().children("ul")
                    )
                    ;
                }
            } else {
                message(data.msg, "请尝试尝试或者联系管理员", "error", 1500);

            }
        }
    })
};


$("#dormHighSearchSaveBtn").click(function () {
    // var loadDiv = load(".content", 99999);
    var test = /^[0-9]*$/;
    var tungNum = $("#tungNumSearch").val();
    var roomNum = $("#roomNumSearch").val();
    localStorage['tungNum'] = tungNum;
    localStorage['roomNum'] = roomNum;
    if (!test.test(tungNum) || !test.test(roomNum)) {
        // $(this).parent().children(".errorMsg").text("只能输入数字!");
        message("只能输入数字!", "请重新输入!", "error", 2000);
    } else {
        addDormList(1, 7, tungNum, roomNum, function () {
            tableChange("#dormListTable");
        });
    }
});


var saveDorm = function () {
    var loadDiv = load(".saveTungNumContent", 99999999);
    var tungId = $("#tungNumSelect").next().val();
    var roomId = $("#roomNumSelect").next().val();
    if (isNull(tungId)) {
        message("栋数不能为空", "请选择栋数", "error", 2500);
    } else if (isNull(roomId)) {
        message("房号不能为空", "请选择房号", "error", 2500);
    } else {
        $.ajax({
            url: "dorm/save",
            type: "post",
            data: {
                "token": localStorage['token'],
                "tungId": tungId,
                "roomId": roomId
            },
            success: function (data) {
                if (data.code === 200) {
                    message("添加成功", "", "success", 2000);
                    load_close(loadDiv);
                    dialog_close();
                } else {
                    errorMessage(data.msg);
                    load_close(loadDiv);
                }
            }
        })
    }
    // console.log(tungId);
    // console.log(roomId);
};
var saveRoom = function (btn) {
    // console.log(1);
    var loadDiv = load("#dormSaveDialog", 99999);
    var roomNum = btn.parent().parent().children(".SHinput").children("input").val();
    if (isNull(roomNum)) {
        message("房间号不能为空", "请重新输入", "error", 2000);
        load_close(loadDiv)
    } else {
        $.ajax({
            url: "dorm/room/save",
            type: "post",
            data: {
                "token": localStorage['token'],
                "roomNum": roomNum
            },
            success: function (data) {
                if (data.code === 200) {
                    message("添加成功", "", "success", 2000);
                    load_close(loadDiv);
                    dialog_close();
                } else {
                    errorMessage(data.msg);
                    load_close(loadDiv);
                }


            }
        })
    }

};

var saveTung = function (btn) {
    // console.log(1);
    var loadDiv = load("#dormSaveDialog", 99999);
    var tungNum = btn.parent().parent().children(".SHinput").children("input").val();
    console.log(tungNum);
    if (isNull(tungNum)) {
        message("栋数不能为空", "请重新输入", "error", 2000);
        load_close(loadDiv)
    } else {
        $.ajax({
            url: "dorm/tung/save",
            type: "post",
            data: {
                "token": localStorage['token'],
                "tungNum": tungNum
            },
            success: function (data) {
                if (data.code === 200) {
                    message("添加成功", "", "success", 2000);
                    load_close(loadDiv);
                    dialog_close();
                } else {
                    errorMessage(data.msg);
                    load_close(loadDiv);
                }


            }
        })
    }

};